package com.hp.gxw.api;

import com.hp.gxw.constant.R;
import com.hp.gxw.u8.EFWebService;
import com.hp.gxw.u8.U8ApiUtils;
import com.hp.gxw.u8.U8WebXMLResponseU8WebXMLResult;
import com.hp.gxw.u8.dto.U8ApiResultDto;
import com.hp.gxw.u8.dto.WarehouseParameter;
import com.hp.gxw.u8.xml.WarehouseXml;
import org.apache.axis.message.MessageElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Component
@RequestMapping("/api/warehouse")
public class WarehouseApi {

    @Autowired
    private U8ApiUtils u8ApiUtils;

	@Autowired
	private EFWebService eFWebServiceLocator;

    @Value("${u8client.serviceId}")
    private String serviceId;

    @Value("${u8client.environment}")
    private String environment;

    @RequestMapping("/register")
    @ResponseBody
    public R warehouse_register(@RequestBody Map<String, Object> map){
        if(map == null){
            return R.error("参数为空");
        }
        WarehouseParameter warehouseParameter = new WarehouseParameter();
        if(map.get("code") == null){
            return R.error("code为必填项");
        }
        warehouseParameter.setCode(map.get("code").toString());
        if(map.get("name") == null){
            return R.error("name为必填项");
        }
        warehouseParameter.setName(map.get("name").toString());
        if(map.get("address") != null){
            warehouseParameter.setAddress(map.get("address").toString());
        }
        if(map.get("person") != null){
            warehouseParameter.setPerson(map.get("person").toString());
        }
        if(map.get("valuestyle") != null){
            warehouseParameter.setValuestyle(map.get("valuestyle").toString());
        }else{
            warehouseParameter.setValuestyle("全月平均法");
        }
        if(map.get("iwhproperty") == null){
            return R.error("iwhproperty为必填项");
        }
        warehouseParameter.setIwhproperty(map.get("iwhproperty").toString());
        if(map.get("phone") != null){
            warehouseParameter.setPhone(map.get("phone").toString());
        }
        warehouseParameter.setIexconmode("0");
        warehouseParameter.setIsaconmode("0");
        warehouseParameter.setIstconmode("0");
        warehouseParameter.setEfserverid(serviceId);
        if(environment.equals("pro")){
            U8WebXMLResponseU8WebXMLResult u8WebXML = null;
            try {
                u8WebXML = eFWebServiceLocator.getEFWebServiceSoap().u8WebXML(WarehouseXml.createXML(warehouseParameter));
                //解析返回结果
                MessageElement[] data = u8WebXML.get_any();
                System.out.println(data[0].getAttribute("succeed").toString());
                System.out.println(data[0].getAttribute("dsc").toString());
                if("0".equals(data[0].getAttribute("succeed").toString())) {
                    return R.error("接口异常"+data[0].getAttribute("dsc").toString());
                }
            }  catch (Exception e) {
                e.printStackTrace();
                return R.error("接口异常");
            }
        }else if(environment.equals("dev")){
            try {
                U8ApiResultDto resultDto = u8ApiUtils.httpRequest(WarehouseXml.createXML(warehouseParameter).split("\n")[1]);
                if(resultDto == null || "0".equals(resultDto.getCode())) {
                    return R.error(resultDto==null?"接口异常":resultDto.getMsg());
                }
            }catch (Exception e){
                e.printStackTrace();
                return R.error("接口异常");
            }
        }
        return R.ok("写入成功");
    }
}
